package top.whysu.edevice.controller.manage;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.whysu.edevice.dto.generate.RequestDTO;
import top.whysu.edevice.dto.generate.ResponseDTO;
import top.whysu.edevice.dto.manage.*;
import top.whysu.edevice.dto.page.PagenationDataDTO;
import top.whysu.edevice.po.manage.ManageVoucherPO;
import top.whysu.edevice.service.manage.ManageVoucherService;
import top.whysu.edevice.utils.ResponseUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 凭证管理
 */
@Controller
@RequestMapping("/manageVoucher")
public class ManageVoucherController {

    @Resource
    private ManageVoucherService manageVoucherService;

    /**
     * 凭证管理界面
     */
    @RequestMapping("/manageVoucher")
    public String deviceManager() {
        return "manage/manageVoucher";
    }

    /**
     * 凭证管理--新增、修改凭证组
     */
    @PostMapping("/addModify")
    @ResponseBody
    public ResponseDTO<BigDecimal> addModifyVoucher(@RequestBody @Validated RequestDTO<VoucherAddModifyDTO> requestDTO) {
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader(), manageVoucherService.wholeAddModifyVoucher(requestDTO.getBody()));
    }

    /**
     * 凭证管理--二维列表
     */
    @RequestMapping("/getPage")
    @ResponseBody
    public ResponseDTO<PagenationDataDTO<ManageVoucherPO>> getPage(@RequestBody VoucherQueryDTO voucherQueryDTO) {
        return ResponseUtil.getSuccessResponse(manageVoucherService.selectPage(voucherQueryDTO));
    }

    /**
     * 凭证管理--删除凭证
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseDTO delete(@RequestBody RequestDTO<BigDecimal> request) {
        manageVoucherService.wholeDeleteVoucher(request.getBody());
        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 凭证管理--点击【分配账号】按钮，弹出的窗口界面
     *
     * @param voucherId 凭证组id
     */
    @RequestMapping(value = "/assignAccount")
    public String assignAccount(String voucherId, Map<String, Object> map) {
        map.put("voucherId", voucherId);
        return "manage/assignAccount";
    }

    /**
     * 凭证管理--分配账号--二维列表
     */
    @RequestMapping("/getAssignAccountPage")
    @ResponseBody
    public ResponseDTO<PagenationDataDTO<AssignAccountPageDTO>> getAssignAccountPage(@RequestBody @Validated AssignAccountQueryDTO queryDTO) {
        return ResponseUtil.getSuccessResponse(manageVoucherService.getAssignAccountPage(queryDTO));
    }

    /**
     * 凭证管理--分配账号--新增关联账号
     */
    @PostMapping("/assignAccountAdd")
    @ResponseBody
    public ResponseDTO assignAccountAdd(@RequestBody @Validated RequestDTO<AssignAccountAddDTO> requestDTO) {
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader(), manageVoucherService.wholeAddAssignAccount(requestDTO.getBody()));
    }

    /**
     * 凭证管理--分配账号--删除关联账号
     *
     * @param requestDTO 这里传的id是e_relative_voucher_account的id
     */
    @PostMapping("/assignAccountDelete")
    @ResponseBody
    public ResponseDTO assignAccountDelete(@RequestBody @Validated RequestDTO<BigDecimal> requestDTO) {
        manageVoucherService.wholeDeleteVoucherAccountRelative(requestDTO.getBody());
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

    /**
     * 获取所有的凭证组列表
     */
    @PostMapping("/getAllVoucherNameList")
    @ResponseBody
    public ResponseDTO<List<VoucherNameDTO>> getAllVoucherNameList(@RequestBody @Validated RequestDTO requestDTO) {
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader(), manageVoucherService.getAllVoucherNameList());
    }

}